package com.qumitech.visualnovel.service.content.impl;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.datasource.console.SpiderSourceMapper;
import com.qumitech.visualnovel.model.dao.console.SpiderSource;

import com.qumitech.visualnovel.service.content.SpiderSourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpiderSourceServiceImpl implements SpiderSourceService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    SpiderSourceMapper spiderSourceMapper;

    @Override
    public List<SpiderSource> getList(String startTime, String endTime, Integer offset, Integer limit, String orderType, String orderBy) {
        return spiderSourceMapper.getList(startTime, endTime, offset, limit, orderType, orderBy);
    }

    @Override
    public Integer getCount(String startTime, String endTime) {
        return spiderSourceMapper.getCount(startTime, endTime);
    }

    @Override
    public JSONObject add(JSONObject param) {
        String link = param.getString("link");
        String subType = param.getString("subType");
        String apiUrl = param.getString("apiUrl");
        Integer type  = param.getInteger("spiderType");
        SpiderSource spiderSource = new SpiderSource();
        spiderSource.setLink(link);
        spiderSource.setSubType(subType);
        spiderSource.setApiUrl(apiUrl);
        spiderSource.setType(type);
        try {
            spiderSourceMapper.insertSelective(spiderSource);
        }catch(Exception e)
        {
            logger.error("method add error:" + e.toString());
            return new BaseResponse(-1,"error");
        }
        return new BaseResponse(0,"success");
    }

    @Override
    public JSONObject update(JSONObject param) {
        Long id = param.getLong("id");
        SpiderSource spiderSource = spiderSourceMapper.selectByPrimaryKey(id);
        String link = param.getString("link");
        String subType = param.getString("subType");
        String apiUrl = param.getString("apiUrl");
        Integer type  = param.getInteger("spiderType");
        spiderSource.setLink(link);
        spiderSource.setSubType(subType);
        spiderSource.setApiUrl(apiUrl);
        spiderSource.setType(type);
        try {
            spiderSourceMapper.updateByPrimaryKeySelective(spiderSource);
        }catch(Exception e)
        {
            logger.error("method update error:" + e.toString());
            return new BaseResponse(-1,"error");
        }
        return new BaseResponse(0,"success");
    }

    @Override
    public JSONObject delete(Long id) {
        try {
            spiderSourceMapper.deleteByPrimaryKey(id);
        }catch(Exception e)
        {
            logger.error("method delete error:" + e.toString());
            return new BaseResponse(-1,"error");
        }
        return new BaseResponse(0,"success");
    }
}
